package Models;

import DB.DBCustomer;
import DB.DBLocation;
import DB.DBRound;
import DB.DBRoute;
import DB.RowInterface;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

public class Location implements RowInterface
{
	private int locationNo;
	private int customerNo;
	private String customerName = "";
	private Customer customer;
	private String address = "";
	private int zipCode;
	private String city = "";
	private Checkpoint checkpoint;
	private ArrayList<Round> rounds = new ArrayList<Round>();
	private int routeNo;
	private Route route;

	public Location ()
	{}

	public void importData(ResultSet data) throws SQLException
	{
		this.setLocationNo(data.getInt("locationNo"));
		this.setCustomerNo(data.getInt("customerNo"));
		this.setCustomerName(data.getString("customerName"));
		this.setAddress(data.getString("address"));
		this.setZipCode(data.getInt("zipCode"));
		this.setCity(data.getString("city"));
		this.setCheckpoint(new Checkpoint(data.getDouble("latitude"), data.getDouble("longitude")));
		this.setRouteNo(data.getInt("routeNo"));
	}

	public void fetchSubItems()
	{
		DBRound dbrd = new DBRound();
		ArrayList<Round> roundRows = dbrd.fetchAll("locationNo = "+getLocationNo());
		if(roundRows == null) roundRows = new ArrayList<Round>();
		this.setRounds(roundRows);
	}

	public void fetchDependentData(int customerNo, int routeNo)
	{
		DBCustomer dbc = new DBCustomer();
		Customer customerRow = dbc.fetchByNo(customerNo);
		if(customerRow == null) customerRow = new Customer();
		this.setCustomer(customerRow);

		DBRoute dbrt = new DBRoute();
		Route routeRow = dbrt.fetchByNo(routeNo);
		if(routeRow == null) routeRow = new Route();
		this.setRoute(routeRow);
	}

	public HashMap exportData()
	{
		HashMap data = new HashMap();
		data.put("customerNo", getCustomerNo());
		data.put("address", getAddress());
		data.put("zipCode", getZipCode());
		data.put("longitude", getCheckpoint().getLongitude());
		data.put("latitude", getCheckpoint().getLatitude());
		data.put("routeNo", getRouteNo());
		return data;
	}

	public void setData(String addressS, String zipCodeS, String longitudeS, String latitudeS) throws EmptyValueException, NumberFormatException
	{
		if(!addressS.equals("") && !zipCodeS.equals("") && !longitudeS.equals("") && !latitudeS.equals(""))
		{
			this.setAddress(addressS);

			int zipCodeTemp = Integer.parseInt(zipCodeS);
			this.setZipCode(zipCodeTemp);

			double latitude = Double.parseDouble(latitudeS);
			double longitude = Double.parseDouble(longitudeS);
			this.setCheckpoint(new Checkpoint(longitude, latitude));
		}
		else
		{
			throw new EmptyValueException();
		}
	}

	public void refreshData()
	{
		Location location = new DBLocation().fetchByNo(getLocationNo());
		this.setAddress(location.getAddress());
		this.setZipCode(location.getZipCode());
		this.setCity(location.getCity());
		this.setCheckpoint(location.getCheckpoint());
		this.setCustomer(location.getCustomer());
		this.setRoute(location.getRoute());
		this.setRounds(location.getRounds());
	}

	public static String[] getSearchColumns()
	{
		return new String[] { "locationNo", "address", "zipCode", "city", "latitude", "longitude", "routeNo" };
	}

	public boolean addRound(Round round)
	{
		round.setLocationNo(locationNo);
		return rounds.add(round);
	}

	public boolean removeRound(Round round)
	{
		int i = 0;
		while(i <= getRounds().size())
		{
			if(round.getRoundNo() == getRounds().get(i).getRoundNo())
			{
				round.setLocationNo(0);
				return rounds.remove(getRounds().get(i));
			}
			i++;
		}
		return false;
	}

	public void setLocationNoOnRounds()
	{
		for(Round roundE : rounds)
		{
			roundE.setLocationNo(locationNo);
		}
	}

	public String getAddress()
	{
		return address;
	}

	public Checkpoint getCheckpoint()
	{
		return checkpoint;
	}

	public String getCity()
	{
		return city;
	}

	public Customer getCustomer()
	{
		return customer;
	}

	public String getCustomerName()
	{
		return customerName;
	}

	public int getCustomerNo()
	{
		return customerNo;
	}

	public int getLocationNo()
	{
		return locationNo;
	}

	public ArrayList<Round> getRounds()
	{
		return rounds;
	}

	public Route getRoute()
	{
		return route;
	}

	public int getRouteNo()
	{
		return routeNo;
	}

	public int getZipCode()
	{
		return zipCode;
	}

	public void setAddress(String address)
	{
		this.address = address;
	}

	public void setCheckpoint(Checkpoint checkpoint)
	{
		this.checkpoint = checkpoint;
	}

	public void setCity(String city)
	{
		this.city = city;
	}

	public void setCustomer(Customer customer)
	{
		this.customer = customer;
		this.setCustomerNo(customer.getCustomerNo());
	}

	public void setCustomerName(String customerName)
	{
		this.customerName = customerName;
	}

	public void setCustomerNo(int customerNo)
	{
		this.customerNo = customerNo;
	}

	public void setLocationNo(int locationNo)
	{
		this.locationNo = locationNo;
	}

	public void setRounds(ArrayList<Round> rounds)
	{
		this.rounds = rounds;
	}

	public void setRoute(Route route)
	{
		this.route = route;
		this.setRouteNo(route.getRouteNo());
	}

	public void setRouteNo(int routeNo)
	{
		this.routeNo = routeNo;
	}

	public void setZipCode(int zipCode)
	{
		this.zipCode = zipCode;
	}
}